package com.cz.background.service.common;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.cz.background.common.exception.BizException;
import com.cz.background.dto.common.menu.AddSysMenuDTO;
import com.cz.background.dto.common.menu.MenuQueryDTO;
import com.cz.background.dto.common.menu.UpdateStatusDTO;
import com.cz.background.dto.common.menu.UpdateSysMenuDTO;
import com.cz.background.entity.common.SysMenu;
import com.cz.background.mapper.common.SysMenuMapper;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;


    public List<SysMenu> queryMenuTree(MenuQueryDTO menuQueryDTO) {
       List<SysMenu> sysMenusList = sysMenuMapper.findByAll(menuQueryDTO);
       return sysMenusList;
    }

    public void delMenu(List<String> ids) {
        if(CollUtil.isEmpty(ids)) {
           throw new BizException("ids不能为空");
        }
        sysMenuMapper.deleteByIdIn(ids);
    }

    public void addMenu(@Valid AddSysMenuDTO addSysMenuDTO) {
       SysMenu sysMenu = BeanUtil.copyProperties(addSysMenuDTO, SysMenu.class);
       sysMenuMapper.insert(sysMenu);
    }

    public void updateMenu(@Valid UpdateSysMenuDTO updateSysMenuDTO) {
        SysMenu sysMenu = BeanUtil.copyProperties(updateSysMenuDTO, SysMenu.class);
        sysMenuMapper.updateByPrimaryKeySelective(sysMenu);
    }

    public void updateStatus(@Valid UpdateStatusDTO updateStatusDTO) {
       sysMenuMapper.updateStatusByIdIn(updateStatusDTO);
    }
}
